<?php

class EventHandler
{
  private $events;
  
  function __construct()
  {
    $this->initialize();
  }
  
  private function initialize()
  {
    $this->events = array();
  }
  
  public function addEvent($event, $func, $appendParams = array())
  {
    $event = md5(strtolower($event));
    if(!isset($this->events[$event])) {
      $this->events[$event] = array();
    }
    $this->events[$event][] = array('func' => $func, 'params' => $appendParams);
    //print_r($this->events);
  }
  
  public function removeEvent($event, $func)
  {
    $event = md5(strtolower($event));
    if(!isset($this->events[$event])) {return false;}
    foreach($this->events[$event] as $k=>$e) {
      if($e['func']) {
        unset($this->events[$event][$k]);
      }
    }
  }
  
  public function removeEvents()
  {
    $this->initialize();
  }
  
  public function fireEvent($event, $params = array())
  {
    if(!is_array($params)) {$params = array($params);}
    $event = md5(strtolower($event));
    //print_r($this->events);
    if(!isset($this->events[$event])) {return false;}
    foreach($this->events[$event] as $e) {
      if(function_exists($e['func'])) {
        if(call_user_func_array($e['func'], merge($params, $e['params'])) == "stop") {
          break;
        }
      }
    }
  }
}
?>